//
//  YZTopScrollView.m
//  tasker
//
//  Created by yangming on 2017/5/31.
//  Copyright © 2017年 BT. All rights reserved.
//

#import "YZTopScrollView.h"

@interface YZTopScrollView()

@property (nonatomic, weak) UIButton *lastSelectedButton;

@property (nonatomic, weak) UIView *indicatorView;

@property (nonatomic, strong) NSMutableArray *buttons;

@end

CGFloat const  kTitleLabelNorimalFont  = 16; // 普通字体大小
CGFloat const  kTitleLabelSelectedFont = 16; // 选中字体大小
CGFloat const  kIndicatorViewHeight    = 3;  // 滑动条高度

@implementation YZTopScrollView

#pragma mark- 初始化View

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self= [super initWithFrame:frame]) {
        
        [self initialization];
    }
    
    return self;
}

#pragma mark- 初始化子控件

- (void)initialization {
    
    // 初始化scrollView
    UIScrollView *scrollView = [self createScrollView];
    self.scrollView = scrollView;
    [self addSubview:self.scrollView];
    
    // 初始化被选中channelButton的红线，也就是indicatorView
    UIView *indicatorView = [self createIndicatorView];
    self.indicatorView = indicatorView;
    [self addSubview:self.indicatorView];
}

// 根据传入的名称集合 创建按钮
- (void)setChannelNameArray:(NSArray *)channelNameArray {
    
    _channelNameArray = channelNameArray;
    CGFloat buttonWidth = self.scrollView.frame.size.width / channelNameArray.count;
    if (channelNameArray.count > 4) {
        
        buttonWidth = self.scrollView.frame.size.width / 4.5;
    }
    
    self.scrollView.contentSize = CGSizeMake(buttonWidth * channelNameArray.count, 0);
    for (NSInteger i = 0; i < channelNameArray.count; i++) {
        
        // 按鈕
        UIButton *button = [self createChannelButton];
        button.frame = CGRectMake(i * buttonWidth, 0, buttonWidth, self.frame.size.height);
        [button setTitle:channelNameArray[i] forState:UIControlStateNormal];
        
        [self.scrollView addSubview:button];
        [self.buttons addObject:button];
    }

    // 选中第一个
    [self clickChannelButton:self.scrollView.subviews[0]];
}

// 创建容纳channelButton的ScrollView
- (UIScrollView *)createScrollView {

    UIScrollView *scrollView = [[UIScrollView alloc] init];
    self.scrollView = scrollView;
    scrollView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.backgroundColor = [UIColor whiteColor];
    
    return scrollView;
}

// 创建被选中channelButton的红线，也就是indicatorView
- (UIView *)createIndicatorView {
    
    UIColor *bgColor = [UIColor clearColor];
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = bgColor;
    [self addSubview:indicatorView];
    
    return indicatorView;
}

// 创建ChannelButton
- (UIButton *)createChannelButton{
    
    UIColor *selectColor = kColorWithHex(0x434343);
    UIColor *bgColor = kColorWithHex(0x7A7A7A);
    CGFloat fontSize = kTitleLabelNorimalFont;

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitleColor:bgColor forState:UIControlStateNormal];
    [button setTitleColor:selectColor forState:UIControlStateDisabled];
    [button.titleLabel setFont:[UIFont systemFontOfSize:fontSize]];
    [button addTarget:self action:@selector(clickChannelButton:) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor clearColor];
    
    return button;
}

- (UIColor *)randomColor {
    
    return [UIColor colorWithRed:(CGFloat)random() / (CGFloat)RAND_MAX
                           green:(CGFloat)random() / (CGFloat)RAND_MAX
                            blue:(CGFloat)random() / (CGFloat)RAND_MAX
                           alpha:1.0f];
}

// 选择了某个ChannelButton
- (void)clickChannelButton:(UIButton *)sender {
    
    CGFloat fontSize = kTitleLabelNorimalFont;
    self.lastSelectedButton.titleLabel.font = [UIFont systemFontOfSize:fontSize];
    self.lastSelectedButton.enabled = YES;
    self.lastSelectedButton = sender;
    self.lastSelectedButton.enabled = NO;
    
    CGFloat newOffsetX = sender.center.x - self.frame.size.width * 0.5;
    if (newOffsetX < 0) {
        newOffsetX = 0;
    }
    if (newOffsetX > self.scrollView.contentSize.width - self.scrollView.frame.size.width) {
        newOffsetX = self.scrollView.contentSize.width - self.scrollView.frame.size.width;
    }

    [sender.titleLabel setFont:[UIFont systemFontOfSize:kTitleLabelSelectedFont]];
    [self.scrollView setContentOffset:CGPointMake(newOffsetX, 0)];
    
    // indicatorView 宽度
    self.indicatorView.frame = CGRectMake(sender.frame.origin.x, self.frame.size.height - kIndicatorViewHeight, sender.frame.size.width, kIndicatorViewHeight);
    
    NSInteger subTag = 1920;
    if (![self.indicatorView viewWithTag:subTag]) {
        
        // 滑动条
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 32, kIndicatorViewHeight)];
        view.center = CGPointMake(self.indicatorView.size.width/2, self.indicatorView.size.height/2);
        view.backgroundColor = kColorWithHex(0x32BE60);
        view.tag = subTag;
        [self.indicatorView addSubview:view];
    }
    
    // 获取按钮下标位置
    NSInteger index = [self.scrollView.subviews indexOfObject:sender];
    if ([self.delegate respondsToSelector:@selector(chooseChannelWithIndex:)]) {
        
        [self.delegate chooseChannelWithIndex:index];
    }
}

#pragma mark - 选中某个ChannelButton

- (void)selectChannelButtonWithIndex:(NSInteger)index {
    
    self.indicatorView.hidden = NO;
    //因为subviews包含indicatorView,所以index需要加1
    [self clickChannelButton:self.scrollView.subviews[index]];
}

- (void)indicatorViewDidScroll:(CGFloat)translationX {
    
//    NSLog(@"translationX:%f", translationX);
    CGRect frame = self.indicatorView.frame;
    float size = self.channelNameArray.count > 4 ? 4.5 : self.channelNameArray.count;
    frame.origin.x = (translationX / size);
    self.indicatorView.frame = frame;
}

#pragma mark - 懶加載
- (NSMutableArray *)buttons{
    
    if (_buttons == nil) {
        _buttons = [NSMutableArray array];
    }
    
    return _buttons;
}

@end












